$(function() {
      $('#post-comment').hide();
      $('#btn-comment').on('click', function(event) {
          event.preventDefault();

          $('#post-comment').slideDown();
      });
     $('#btn-like').on('click', function() {
         var self = this;
         fetch('/images/like', {
            method: 'POST',
             headers: {
                 'Content-Type': 'application/json'
             },
             body: JSON.stringify({imageid: $(self).data('id')})
         })
         .then(function(response) {
             return response.json();
         })
         .then(function(obj) {
             $('strong.like-count').text(obj.likes);
             var likes = $('.allLikes').html();
            $('.allLikes').text(Number(likes)+1);
         })
         .catch(function(err) {
             alert(err)
        })
    })

     $('#btn-delete').on('click', function() {
        fetch(location.href, {method: 'DELETE'})
        .then(function(response) {
            return response.json()
        })
        .then(function(obj) {
            $('#btn-delete').empty().text('已删除！').attr('disabled', true)
             $('#btn-like').attr('disabled', true)
             $('#comment-btn').attr('disabled', true)

             setTimeout("window.location.href='http://127.0.0.1:3000/'",1000);
         })
     })

     timeago().render(document.querySelectorAll('.need_to_be_rendered'),'zh_CN');
 })
